<template>
  <a-drawer
    :title="title"
    :maskClosable="true"
    :width="drawerWidth"
    placement="right"
    :closable="true"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @close="handleCancel"
  >
    <template slot="title">
      <div style="width: 100%">
        <span>{{ title }}</span>
        <span style="display: inline-block; width: calc(100% - 51px); padding-right: 10px; text-align: right">
          <a-button @click="toggleScreen" icon="appstore" style="height: 20px; width: 20px; border: 0px"></a-button>
        </span>
      </div>
    </template>

    <a-spin :spinning="confirmLoading">
      <a-form-model ref="form" :model="model" :rules="validatorRules">
        <a-form-model-item :labelCol="labelCol" :wrapperCol="wrapperCol" prop="tagType" required label="分类">
          <a-select placeholder="请选择" v-model="tagType" @change="handleSelectChange">
            <a-select-option v-for="(item, index) in category" :key="index" :value="item.dataId">
              {{ item.categoryName }}
            </a-select-option>
          </a-select>
        </a-form-model-item>
        <a-form-model-item :labelCol="labelCol" :wrapperCol="wrapperCol" prop="tagName" required label="标签">
          <a-input placeholder="请输入标签" v-model="model.tagName" :maxLength="50" />
        </a-form-model-item>
        <a-form-model-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="热度">
          <a-input-number placeholder="请输入热度" :min="0" v-model="model.tagHot" />
        </a-form-model-item>
      </a-form-model>
    </a-spin>

    <div class="drawer-bootom-button" v-show="!disableSubmit">
      <a-button style="margin-right: 0.8rem" @click="handleCancel">取消</a-button>
      <a-button @click="handleOk" type="primary" :loading="confirmLoading">提交</a-button>
    </div>
  </a-drawer>
</template>

<script>
import Vue from 'vue'
import { USER_INFO } from '@/store/mutation-types'
import { getAction, postAction } from '@/api/manage'

export default {
  name: 'TagModal',
  data() {
    return {
      drawerWidth: 700,
      disableSubmit: false,
      value: 1,
      title: '操作',
      visible: false,
      category: [],
      tagType: undefined,
      model: {},
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 }
      },
      confirmLoading: false,
      validatorRules: {
        tagType: [{ required: true, message: '请选择分类!' }],
        tagName: [{ required: true, message: '请输入标签!' }]
      },
      serviceCategory: []
    }
  },
  created() {},
  methods: {
    add() {
      this.edit({})
    },
    edit(record) {
      this.visible = true
      this.getCategory(record.type)
      if (record.type) {
        console.log(111)
        this.model = record
        this.tagType = record.tagType
      }
      console.log(this.tagType)
      this.resetScreenSize()
    },
    getCategory(id) {
      postAction('tag/category/list', {}).then(res => {
        if (res.success) {
          this.category = res.result.filter(v => {
            return v.categoryPid != 0
          })
        } else {
          console.log(res.message)
        }
      })
    },
    handleSelectChange(value) {
      this.category.forEach(v => {
        if (v.dataId == value) {
          this.model.tagTypeName = v.categoryName
        }
      })
      console.log(this.tagType)
      this.model.tagType = value
    },
    // 根据屏幕变化,设置抽屉尺寸
    resetScreenSize() {
      let screenWidth = document.body.clientWidth
      if (screenWidth < 500) {
        this.drawerWidth = screenWidth
      } else {
        this.drawerWidth = 700
      }
    },
    //窗口最大化切换
    toggleScreen() {
      if (this.modaltoggleFlag) {
        this.modalWidth = window.innerWidth
      } else {
        this.modalWidth = 800
      }
      this.modaltoggleFlag = !this.modaltoggleFlag
    },
    // 确定
    handleOk() {
      const that = this
      // 触发表单验证
      that.$refs.form.validate(valid => {
        if (valid) {
          that.confirmLoading = true
          delete that.model.type
          console.log(that.model)
          postAction('tag/saveOrUpdate', that.model)
            .then(res => {
              console.log('res=======', res)
              if (res.success) {
                that.$message.success(res.message)
                that.$emit('ok')
              } else {
                that.$message.warning(res.message)
              }
            })
            .finally(() => {
              that.confirmLoading = false
              that.close()
            })
        } else {
          return false
        }
      })
    },
    // 关闭
    handleCancel() {
      this.close()
    },
    close() {
      this.$emit('close')
      this.visible = false
      this.disableSubmit = false
      this.model = {}
      this.tagType = undefined
      // this.$refs.form.resetFields()
    }
  }
}
</script>

<style scoped>
.avatar-uploader > .ant-upload {
  width: 104px;
  height: 104px;
}
.ant-upload-select-picture-card i {
  font-size: 49px;
  color: #999;
}

.ant-upload-select-picture-card .ant-upload-text {
  margin-top: 8px;
  color: #666;
}

.ant-table-tbody .ant-table-row td {
  padding-top: 10px;
  padding-bottom: 10px;
}

.drawer-bootom-button {
  position: absolute;
  bottom: -8px;
  width: 100%;
  border-top: 1px solid #e8e8e8;
  padding: 10px 16px;
  text-align: right;
  left: 0;
  background: #fff;
  border-radius: 0 0 2px 2px;
}
.ant-input-number {
  width: 100%;
}
</style>

